<template>
	<om-formsub-outer :index="index" :checkedit="checkedit" :deleteBtn="isDel">
		<om-autoform @switchChange = "switchChange" ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				handleUserIdCacheKey: 'hr-u-tlApproval-handleUserId-cache',
				isDel: false,
				hamdleList: {},
				otherItems: '',
				az: '',
				currentUserId: '',
				infonameId:""
			})
		},
		methods:{
			getHandlePostList(id){
				let _that = this
				_that.om.request('tlApproval/getHandlePostList', {
					itemId: _that.$refs.autoform.formdata.itemId,
					userId: id,
					leaveUserId: _that.currentUserId
				}).then(zres => {
					if(zres.length == 1){
						_that.$refs.autoform.formdata.handlePosId = zres[0].id
						_that.$refs.autoform.formdata.handlePosName = zres[0].text
						_that.$refs.autoform.formdata.handleInstName = zres[0].attributes.instName
					}
					_that.$refs.autoform.changeEditor("handleInfo", "handlePosId", "data", configSelectData(zres,"id","text"))
					_that.$refs.autoform.initSelectIndex("handleInfo", "handlePosId")
				})
			},
			verifySubConfirm(){
				let _that = this
				if(_that.$refs.autoform.formdata.deductionNum && _that.$refs.autoform.formdata.deductionNum<0){
					_that.$refs.autoform.formdata.deductionNum = 0
					let title = _that.$refs.autoform.getEditorTitle("handleInfo","deductionNum")
					_that.om.toast(title+'必须大于0')
				}else{
					return true
				}
			},
			switchChange(value){
				let _that = this
				_that.$refs.autoform.formdata.checkFlag = value
			},
			verifySubDel(){
				let _that = this
				let value = false
				if(_that.az == '成功'){
					value = true
				}else{
					_that.om.toast(_that.az);
					value = false	
				}
				return value
			},
			onSelectHandlePosName(data){
				let _that = this
				_that.$refs.autoform.formdata.handleInstName = data.attributes.instName
			},
			clearHandlePosName(){
				let _that = this
				_that.$refs.autoform.clear("handleInfo","handlePosId")
				_that.$refs.autoform.clear("handleInfo","handleInstName")
			},
			clearHandleUserName(){
				let _that = this
				_that.$refs.autoform.clear("handleInfo","handleUserId")
				_that.$refs.autoform.clear("handleInfo","handlePosId")
				_that.$refs.autoform.clear("handleInfo","handleInstName")
			},
			onSelectItemName(data){
				let _that = this
				_that.$refs.autoform.formdata.handleUserId = data.handleUserId
				_that.$refs.autoform.formdata.handleUserName = data.handleUserName
				_that.getHandlePostList(data.handleUserId)
				_that.$refs.autoform.changeEditor('handleInfo', 'handleUserId', {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							itemId: data.itemId,
							leaveUserId: _that.currentUserId
						}),
						namefield: "text",
						initload:true,
						url: "/tlApproval/getHandleUserList",
						cachename: _that.handleUserIdCacheKey,
						placeholder: "请输入姓名"
					}
				}, null)
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: _that.handleUserIdCacheKey,
					success: function(res) {
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.handleUserId = d.id
						formdata.handleUserName = d.text
						_that.getHandlePostList(d.id)
						uni.removeStorage({
							key: _that.handleUserIdCacheKey
						});
					}
				});
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.infonameId = e.infonameId
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								if(e.isCheck){
									for(let item of res.data){
										item.editors = item.editors.filter(d=>{
											if(d.valueField == "itemId"){
												d.type = 'labelbox'
											}
											if(d.valueField == "handleRemark"){
												d.type = 'lookfilebox'
											}
											return d
										})
									}
								}
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								if(e.isCheck){
									changeEditor("handleInfo","checkFlagName","title","是否交接",_that.autoformdata)
								}
								changeEditor("handleInfo",'handleRemark',"isText",true,_that.autoformdata)
								changeEditor("handleInfo",'handleRemark',"disabled",true,_that.autoformdata)
								changeEditor("handleInfo","handleRemark","remark",_that.index ? _that.datas[_that.index].handleRemark : '',_that.autoformdata)
								changeEditor("handleInfo",'handleRemark',"showClear",false,_that.autoformdata)
								changeEditor("handleInfo",'handleRemark',"isText",true,_that.autoformdata)
								
								if(_that.infonameId == _that.userId){
									changeEditor("handleInfo","itemId","type","labelbox",_that.autoformdata)
								}
								
								if(e.isCheck){
									if (_that.formdata.handleUserId==_that.userId){
									}else {
									    changeEditor("handleInfo","deductionNum","type","labelbox",_that.autoformdata)
									}
								}else{
									changeEditor("handleInfo","deductionNum","type","labelbox",_that.autoformdata)
								}
								
								_that.currentUserId = e.infonameId
								_that.hamdleList = uni.getStorageSync('hamdleListKey')
								if(e.showDel == "false"){
									_that.isDel = false
								}else{
									_that.isDel = _that.hamdleList.isDel
								}
								let zdata = ""
								if(_that.index || _that.index == 0){
									if(_that.index+'' || _that.index == 0){
										zdata = _that.datas.filter(e => e.itemId != _that.datas[_that.index].itemId);
									}else{
										zdata = _that.datas.filter(e => e.itemId);
									}
								}else{
									zdata = _that.datas.filter(e => e.itemId);
								}
								
								_that.otherItems = zdata.map(e=>e.itemId).join(",")
								
								_that.om.request(
									"tlApproval/isDeductionItem",
									{
										itemId:_that.formdata.itemId
									}
								).then(res=>{
									if(res.succeed){
										if(res.value){
											changeEditor("handleInfo","deductionNum","type","numberbox",_that.autoformdata)
										}else{
											changeEditor("handleInfo","deductionNum","type","labelbox",_that.autoformdata)
										}
									}
								})
								_that.om.request('tlApproval/getHandleUserList', {
									itemId:_that.formdata.itemId,
									leaveUserId: _that.currentUserId
								}).then(res => {
									if(res.length == 1){
										let d = res[0]
										var formdata  = _that.$refs.autoform.formdata
										formdata.handleUserId = d.id
										formdata.handleUserName = d.text
										_that.getHandlePostList(d.id)
									}
								})
								_that.om.request('commonForm/hasMainPos', {
									busCode:"HRS12"
								},'post').then(res => {
									//当前为经办人或上级主管
									if(res.value == true || _that.userId == _that.formdata.handleUserId){
										
									}else{
										_that.$refs.autoform.changeEditor("handleInfo","checkFlagName","type","labelbox")
										_that.$refs.autoform.changeEditor("handleInfo","handleUserRemark","type","labelbox")
									}
								})
								
								changeEditor('handleInfo', 'handleUserId', {
									link: "../../public/search/search",
									"linkParam": {
										other: JSON.stringify({
											itemId: _that.formdata.itemId,
											leaveUserId: _that.currentUserId
										}),
										namefield: "text",
										initload:true,
										url: "/tlApproval/getHandleUserList",
										cachename: _that.handleUserIdCacheKey,
										placeholder: "请输入姓名"
									}
								}, null, _that.autoformdata)
								
								_that.$nextTick(function(){									
									_that.om.request('tlApproval/getItemCombobox', {
										leaveUserId: _that.currentUserId,
										itemIdsStr: _that.otherItems,
									}).then(res => {
										let editor = _that.$refs.autoform.getEditor("handleInfo", "itemId")
										if(editor.type == 'selectbox'){
											_that.$refs.autoform.changeEditor("handleInfo", "itemId", "data", configSelectData(res,"itemId","itemName"))
											_that.$refs.autoform.initSelectIndex("handleInfo", "itemId")
										}
									})
									
									if(_that.$refs.autoform.formdata.itemId){
										_that.om.request('tlApproval/verifyRomveItem', {
											itemId: _that.$refs.autoform.formdata.itemId,
										}).then(res => {
											if(res.succeed == true){
												_that.az = '成功'
											}else{
												_that.az = res.msg
											}
										})
									}
									_that.om.request('tlApproval/getHandlePostList', {
										itemId: _that.$refs.autoform.formdata.itemId,
										userId: _that.$refs.autoform.formdata.handleUserId,
										leaveUserId: _that.currentUserId
									}).then(res => {
										_that.$refs.autoform.changeEditor("handleInfo", "handlePosId", "data", configSelectData(res,"id","text"))
										_that.$refs.autoform.initSelectIndex("handleInfo", "handlePosId")
										if(uni.getStorageSync('isparentUserKey') == 0){
											_that.$refs.autoform.changeEditor("handleInfo","itemId","type","labelbox")
											_that.$refs.autoform.changeEditor("handleInfo","handleUserId","type","labelbox")
											_that.$refs.autoform.changeEditor("handleInfo","handlePosId","type","labelbox")
										}
									})
								})
							}
						}
					})
				}				
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
